package cn.pzaily.orange.core.utils.convert;


import cn.pzaily.orange.core.utils.ObjectUtils;
import cn.pzaily.orange.core.utils.StringUtils;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

/**
 * @PackageName: cn.pzaily.common.core.text
 * @Description:
 * @Author: Pang
 * @Date: 2021-03-06 16:59
 */
public class ConvertUtils {


    public static Integer toInt(Object value, Integer defaultValue) {
        if (ObjectUtils.isNull(value)) {
            return defaultValue;
        }
        if (value instanceof Integer) {
            return (Integer) value;
        }
        if (value instanceof Number) {
            return ((Number) value).intValue();
        }
        String valueStr = toStr(value, null);
        if (StringUtils.isEmpty(valueStr)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(valueStr);
        } catch (Exception e) {
            return defaultValue;
        }

    }

    public static Integer toInt(Object value) {
        return toInt(value, null);
    }


    public static String toStr(Object value) {
        return toStr(value, null);
    }

    public static String toStr(Object value, String defaultValue) {
        if (ObjectUtils.isNull(value)) {
            return defaultValue;
        }
        if (value instanceof String) {
            return (String) value;
        }
        return value.toString();
    }


    public static String utf8Str(Object params) {
        return str(params, CharseKit.CHARSET_UTF_8);
    }

    public static String str(Object obj, String charseName) {
        return str(obj, Charset.forName(charseName));
    }

    public static String str(Object obj, Charset charset) {
        if (null == obj) {
            return null;
        }

        if (obj instanceof String) {
            return (String) obj;
        } else if (obj instanceof byte[] || obj instanceof Byte[]) {
            return str((byte[]) obj, charset);
        } else if (obj instanceof ByteBuffer) {
            return str((ByteBuffer) obj, charset);
        }
        return obj.toString();
    }

    public static String str(byte[] bytes, String charset) {
        return str(bytes, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
    }

    public static String str(byte[] bytes, Charset charset) {
        if (null == bytes) {
            return null;
        }
        if (null == charset) {
            return new String(bytes);
        }
        return new String(bytes, charset);
    }

    public static String str(ByteBuffer byteBuffer, String charset) {
        return str(byteBuffer, StringUtils.isEmpty(charset) ? Charset.defaultCharset() : Charset.forName(charset));
    }

    public static String str(ByteBuffer byteBuffer, Charset charset) {
        if (null == byteBuffer) {
            return null;
        }
        if (null == charset) {
            charset = Charset.defaultCharset();
        }
        return charset.decode(byteBuffer).toString();
    }
}
